<?php

uiLoadLibrary('editor');


class cUiClientPrivs extends cUiFormItem
{
	function __construct($user, $argv){
		global $gPrivString, $gSimplePrivs;
		
		parent::__construct('Privileges');
		
		
	}
}

class modUserMgr extends cDycmsClientModule
{
	function __construct()
	{
		$mgr = new cUiManager('$modUserMgr',
            array('User Name', 'User ID', 'Privileges'));

        $unum = $mgr->GetObjectId();

		switch($mgr->GetAction())
		{
			case 'save':

			//
			// Save user details
			//
			
			if($unum)
				$user = new cDycmsClient($unum);
			else
				$user = new cDycmsClient(0);
	
			if($user->Edit($_POST['$modUserMgr-uid'],
					$_POST['$modUserMgr-name'],
					$_POST['$modUserMgr-parent'],
					$_POST['$modUserMgr-privs'],
					explode(',', $_POST['$modUserMgr-caps']),
					(($_POST['$modUserMgr-pwd'] == '')
						? null : $_POST['$modUserMgr-pwd'])))
			{
				parent::__construct(uiGetIcon('user_edit').' Editing User');
				$this->DisplayUserForm($user, 'User Details Saved');
			};

			break;
			
            case 'create':
			case 'edit':

			//
			// Edit/create user:
			// Display the user details form.
			//
			
			if($unum) {
				parent::__construct(uiGetIcon('user_edit').' Editing User');
				$user = new cDycmsClient($unum);
			} else {
				parent::__construct(uiGetIcon('user_add').' New User');
				$user = new cDycmsClient(0);
			};

			$this->DisplayUserForm($user);

			break;


			case 'dels':
			
			//
			// Delete selected users
			//
			
			


			break;


			case 'del':

			$user = new cDycmsClient($unum);

			parent::__construct(uiGetIcon('user_delete').'Delete User');

			$this->AddChild('<b>Warning - this action is irreversible.</b><br/>Are you sure you want to delete \''
				.$user->GetUserName().'\'?');
			$this->AddChild($f = new cUiForm());
			$f->AddChild($btnset = new cHtmlWrapper('<div>','</div>'));

			$btnset->AddChild(new cUiButton(array(
					'name' => '$modUserMgr-action',
					'value' => 'delConfirm-'.$unum
			),'Yes', 'accept'));
			$btnset->AddChild(new cUiButton(array(
					'name' => '$modUserMgr-action',
					'value' => ''
			),'No', 'cancel'));

			break;


			case 'delConfirm':

			$user = new cDycmsClient($unum);
			$user->Delete();
			header('Location: '.$_SERVER['REQUEST_URI']);

			break;


			default:
			parent::__construct(uiGetIcon('user').' Manage Users');

			$cls = dbReadAllClients();

            $mgr->CreateForm(dbCountClients());
			$this->AddChild($mgr);

			foreach($cls as $cl) {
				$mgr->AddRow($cl['unum'], array(
				    $cl['name'],
				    $cl['uid'],
				    '0x'.dechex($cl['privs'])));
			};
		}; // switch
	}
	
	function DisplayUserForm($user, $msg = null){
		global $gSimplePrivs;
		
		$this->AddChild($f = new cUiForm(array(
			'id' => '$modUserMgr-'.$user->GetUserNum()
		)));

		if($msg) $f->AddChild(new cUiCompletionMsg($msg));
		
		$f->AddChild(new cUiTextBox(array(
			'name' => '$modUserMgr-uid',
			'value'=>$user->GetUserId()
			), 'User ID'));
		$f->AddChild(new cUiTextBox(array(
			'name' => '$modUserMgr-name',
			'value'=>$user->GetUserName()
			), 'Display Name'));
		$f->AddChild(new cUiTextBox(array(
			'name' => '$modUserMgr-pwd',
			'value'=> ''
			), 'Password (enter to set/change)'));
		
		
		// Privileges
		$f->AddChild(new cUiSelection(array(
			'name' => '$modUserMgr-privs',
			'value' => $user->GetPrivs()
		),$gSimplePrivs,'Privileges'));
		
		// Capabilities
		$f->AddChild(new cUiTextBox(array(
			'name' => '$modUserMgr-caps',
			'value' => implode(',',$user->GetCapabilities())
			), 'Capabilities'));

		// Parent user
		$f->AddChild(new cUiClientSelection(array(
				'name' => '$modUserMgr-parent',
				'value' => $user->GetParentNum()
			), array(
				'allowNull' => true
			),'Superior'));

		

		$f->AddChild($btnset = new cDycmsClientNode());

		$btnset->AddChild(new cUiButton(array(
				'name' => '$modUserMgr-action',
				'value' => 'save-'.$user->GetUserNum(),
		), 'Save', 'user_go'));
		$btnset->AddChild(new cUiButton(array(
				'name' => '$modUserMgr-action',
				'value' => '',
		), 'Cancel', 'cancel'));
		
		// Client side scripts
		cUiPage::GetInst()->AddScript('editor');
		cUiPage::GetInst()->AddScript('admin');
		cUiPage::GetInst()->AddStartScript('$modUserMgr-'.$user->GetUserNum(),
			uiStdWidgetStartScript('modEditUser', '$modUserMgr-'.$user->GetUserNum()));
			
		// Meta data
		cUiPage::GetInst()->AddMetaData('objectId',
			$user->GetUserNum() ? $user->GetUserNum() : 0);
	}
}

